package com.example.newland.fans;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imageView;
    private AnimationDrawable adimation;
    private Button main_but_start;
    private int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_but_start = findViewById(R.id.main_but_start);
        imageView = findViewById(R.id.main_iv_anima_lisst);

        main_but_start.setOnClickListener(this);

        //找到动画资源
        adimation = (AnimationDrawable) imageView.getBackground();
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.main_but_start:
                i++;
                break;
        }
        if(1 == i){
            //打开风扇
            adimation.start();
        }else if(i == 2){
            //关闭风扇
            adimation.stop();
            i = 0;
        }

    }
}
